package com.linkhub.oms.admin.modules.user.accountrecharge.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.google.common.collect.Lists;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.admin.modules.user.accountrecharge.domain.param.*;
import com.linkhub.oms.admin.modules.user.accountrecharge.domain.vo.UserRechargeRecordDetailVO;
import com.linkhub.oms.admin.modules.user.accountrecharge.domain.vo.UserRechargeRecordVO;
import com.linkhub.oms.admin.modules.user.accountrecharge.service.IUserRechargeRecordService;
import com.linkhub.oms.admin.modules.user.domain.param.BaseUserParam;
import com.linkhub.oms.admin.modules.user.domain.vo.MerchantBankCardVO;
import com.linkhub.oms.admin.modules.user.domain.vo.ReceiveBankCardVo;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.common.response.PageDTO;
import com.linkhub.oms.dao.mpservice.IExchangeRateService;
import com.linkhub.oms.entity.ExchangeRateEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 用户账户模块
 *
 * @author Frank
 */
@Slf4j
@RestController
@Api(tags = {"用户账户_充值模块接口"})
@RequestMapping("/user/account/recharge")
@SaCheckLogin
public class UserAccountRechargeController {
    @Resource
    private IUserRechargeRecordService userRechargeRecordService;
    @Resource
    private IExchangeRateService exchangeRateService;

    @GetMapping()
    @ApiOperation(value = "充值列表")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE, RoleConstant.FINANCE}, mode = SaMode.OR)
    public DataResponse<PageDTO<UserRechargeRecordVO>> getRechargeRecordPage(@Validated UserAccountRechargeQueryParam queryParam) {
        queryParam.setUserId(LhSessionContext.getUserId());
        queryParam.setCustomRoleFlag(LhSessionContext.isCustomerRole());
        return DataResponse.success(userRechargeRecordService.getRechargeRecordPage(queryParam));
    }

    @GetMapping("/{transactionId}")
    @ApiOperation(value = "充值详情")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE, RoleConstant.FINANCE}, mode = SaMode.OR)
    public DataResponse<UserRechargeRecordDetailVO> getRechargeRecordDetail(@PathVariable("transactionId") String transactionId) {
        return DataResponse.success(userRechargeRecordService.getRechargeRecordDetail(transactionId));
    }

    @PostMapping()
    @ApiOperation(value = "提交充值")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<Void> submitRecharge(@Valid @RequestBody UserRechargeRecordAddParam userRechargeRecordAddParam) {
        userRechargeRecordAddParam.setUserId(LhSessionContext.getUserId());
        userRechargeRecordService.submitRecharge(userRechargeRecordAddParam);
        return DataResponse.success();
    }

    @PutMapping("/suc/{id}")
    @ApiOperation(value = "充值审核通过")
    @SaCheckRole(value = {RoleConstant.FINANCE})
    public DataResponse<Void> auditRechargeSuc(@PathVariable("id") Long id,
                                               @Valid @RequestBody UserRechargeRecordUpdParam userRechargeRecordUpdParam) {
        userRechargeRecordUpdParam.setUserId(LhSessionContext.getUserId());
        userRechargeRecordUpdParam.setId(id);
        userRechargeRecordService.auditRechargeSuc(userRechargeRecordUpdParam);
        return DataResponse.success();
    }

    @PutMapping("/fail/{id}")
    @ApiOperation(value = "充值审核拒绝")
    @SaCheckRole(value = {RoleConstant.FINANCE})
    public DataResponse<Void> auditRechargeFail(@PathVariable("id") Long id,
                                                @Valid @RequestBody UserRechargeRecordRefuseParam userRechargeRecordRefuseParam) {
        userRechargeRecordRefuseParam.setUserId(LhSessionContext.getUserId());
        userRechargeRecordRefuseParam.setId(id);
        userRechargeRecordService.auditRechargeFail(userRechargeRecordRefuseParam);
        return DataResponse.success();
    }

    @GetMapping("/bank/statement/check")
    @ApiOperation(value = "银行流水校验")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE, RoleConstant.FINANCE}, mode = SaMode.OR)
    public DataResponse<Boolean> checkBankStatement(UserRechargeRecordCheckParam userRechargeRecordCheckParam) {
        return DataResponse.success(userRechargeRecordService.checkBankStatement(userRechargeRecordCheckParam));
    }

    @GetMapping("/bank/card/pay")
    @ApiOperation(value = "查询付款银行卡")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE, RoleConstant.FINANCE}, mode = SaMode.OR)
    public DataResponse<List<MerchantBankCardVO>> getPayBankCard(@RequestParam(required = false) String merchantUserId) {
        BaseUserParam baseUserParam = new BaseUserParam();
        baseUserParam.setUserId(LhSessionContext.getUserId());
        baseUserParam.setCustomRoleFlag(LhSessionContext.isCustomerRole());
        return DataResponse.success(userRechargeRecordService.getPayBankCard(merchantUserId, baseUserParam));
    }

    @GetMapping("/bank/card/receive")
    @ApiOperation(value = "查询收款银行卡_非商家")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE, RoleConstant.FINANCE}, mode = SaMode.OR)
    public DataResponse<List<ReceiveBankCardVo>> getReceiveBankCard() {
        /*
        公司银行账号
        Account Name SHIPO LIMITED
        Account No. 691-577282-838
        Address RooM 2502B, No. 148, Electric Road, North Point, HongKong

        Bank Name HSBC Hong Kong
        Bank Code 004
        Bank Address 1 Queen's Road Central, HongKong
        SWIFT HSBCHKHHHKH
        */
        ReceiveBankCardVo receiveBankCardVo = new ReceiveBankCardVo();
        receiveBankCardVo.setId(1L);
        receiveBankCardVo.setAccount("SHIPO LIMITED");
        receiveBankCardVo.setBankName("HSBC Hong Kong");
        receiveBankCardVo.setCardNumber("691-577282-838");
        receiveBankCardVo.setAccountAddress("RooM 2502B, No. 148, Electric Road, North Point, HongKong");
        receiveBankCardVo.setBankCode("004");
        receiveBankCardVo.setBankAddress("1 Queen's Road Central, HongKong");
        return DataResponse.success(Lists.newArrayList(receiveBankCardVo));
    }

    @GetMapping("/exchange/rate")
    @ApiOperation(value = "查询当日汇率_返回所有币种")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE, RoleConstant.FINANCE}, mode = SaMode.OR)
    public DataResponse<Map<String, String>> getTodayExchangeRate(@RequestParam(required = false) String date) {
        LocalDate dateInput;
        //查询指定日期
        if (StringUtils.isNotBlank(date)) {
            dateInput = LocalDate.parse(date);
            List<ExchangeRateEntity> exchangeRateEntities = exchangeRateService.listExchangeRate(dateInput);
            if (CollUtil.isEmpty(exchangeRateEntities)) {
                return DataResponse.success(MapUtil.newHashMap());
            }
            return DataResponse.success(exchangeRateEntities.get(0).getExchangeRate());
        }
        //查询最近日
        List<ExchangeRateEntity> exchangeRateEntities = exchangeRateService.listExchangeRate(null);
        if (CollUtil.isEmpty(exchangeRateEntities)) {
            return DataResponse.success(MapUtil.newHashMap());
        }
        return DataResponse.success(exchangeRateEntities.get(0).getExchangeRate());

    }

}
